package nl.ru.rd.facedetection.nnbfd;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import nl.ru.rd.facedetection.nnbfd.neuralnetwork.NeuralNetwork;

/**
 * A Testcase showing an Image and a copy of the Image, with the results of the NeuralNetwork drawn on the copy.
 * 
 * @author Christiaan Thijssen
 */
public class Testcase
{
	public static void main(String[] args)
	{
		BufferedImage image = null;
		try
		{
			image = ImageIO.read(new File("test.jpg"));
		} catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		new ImageViewer(image);
		NeuralNetwork test = NeuralNetwork.readFromFile("network999");
		Windows windows = new Windows(image);

		ArrayList<Window> windowList = windows.getWindows();
		ArrayList<Window> successWindows = new ArrayList<Window>();

		System.out.println("Normalizing and checking " + windowList.size() + " windows");

		Matrix matrix = null;
		Window window = null;
		for(int i = 0; i < windowList.size(); i++)
		{
			window = windowList.get(i);
			matrix = window.getMatrix();
			Preprocessor.equalize(matrix);
			double[] value = test.calculate(matrix);
			// System.out.println(value[0]);
			if(value[0] > 0.5)
				successWindows.add(window);
		}

		new ImageViewer(image, successWindows);
		/*
		 * for(int i = 0; i < successWindows.size(); i++) try { ImageIO.write(successWindows.get(i).getMatrix().toImage(), "gif", new File("test/"+i+".gif")); }
		 * catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
		 */
		System.out.println("Found " + successWindows.size() + " windows");
	}

}